<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户模型类
 */
class Agency_model extends CI_Model
{
	public function get($agencyid)
	{
		$agencyid = intval($agencyid);
		$row = $this->db->get_where('h_second_agency', array('agencyid' => $agencyid))->row_array();
		return $row;
	}

	public function get_data($agencyid)
	{
		$agencyid = intval($agencyid);
		$this->db->select('a.*, d.*');
		$this->db->from('h_second_agency AS a');
		$this->db->join('h_second_agency_data AS d', 'a.agencyid = d.agencyid', 'left');
		$row = $this->db->where('a.agencyid', $agencyid)->get()->row_array();
		return $row;
	}

	/**
	 * 中介基本信息表更新
	 */
	public function update($data)
	{
		if (isset($data['agencyid']) && $data['agencyid'])
		{
			$agencyid = $data['agencyid'];
			unset($data['agencyid']);
			$this->db->where('agencyid', $agencyid);
			$this->db->update('h_second_agency', $data);
		}
	}

    /**
	 * 中介数据信息表更新
	 */
	public function update_data($data)
	{
		if (isset($data['agencyid']) && $data['agencyid'])
		{
			$agencyid = $data['agencyid'];
			unset($data['agencyid']);
			$this->db->where('agencyid', $agencyid);
			$this->db->update('h_second_agency_data', $data);
		}
	}

	/**
	 * 获取所有信息（分页显示）
	 *
	 * @param $where array
	 * @param $limit int
	 * @param $offset int
	 * @param $orderby string ('uid DESC, created ASC')
	 * @return array
	 */
	public function get_all_list($where = FALSE, $limit = FALSE, $offset = FALSE, $orderby = FALSE)
	{
		$this->_where_list($where);
		$this->db->order_by($orderby);
		$this->db->limit($limit, $offset);
		$result = $this->db->get('h_second_agency')->result_array();
		return $result;
	}

	public function count_all_list($where)
	{
		$this->_where_list($where);
		return $this->db->count_all_results('h_second_agency');
	}

	private function _where_list($where)
	{
		//管理员帐号
		if (isset($where['adminname']) && $where['adminname'])
		{
			$adminname = $where['adminname'];
			$this->db->where('adminname', $adminname);
		}
		//中介名称
		if (isset($where['name']) && $where['name'])
		{
			$name = $where['name'];
			$this->db->like('name', $name);
		}
	}

	public function get_right($agencyid)
	{
		$this->db->where('agencyid', $agencyid);
		$results = $this->db->get('h_second_agency_right')->result_array();

		$right = array();
		foreach ($results as $row)
		{
			if ($row['vtype'] == 'array')
			{
				$row['rvalue'] = unserialize($row['rvalue']);
			}
			$right[$row['rkey']] = $row['rvalue'];
			//更优写法
			//$right[$row['rkey']] = $row['vtype'] == 'array' ? unserialize($row['rvalue']) : $row['rvalue'];
		}

		return $right;
	}

	public function replace_right($data)
	{
		//根据权限配置文件，不存在的权限键名删除
		$this->load->config('web_right', TRUE);
		$user_right = $this->config->item('user', 'web_right');
		foreach ($data as $k => $v)
		{
			if (array_key_exists($k, $user_right) == TRUE || $k != 'agencyid')
			{
				$replace['agencyid'] = $data['agencyid'];
				//$replace['rtype'] = $user_right[$k][1];
				$replace['rkey'] = $k;
				$replace['rvalue'] = $v;
				$replace['vtype'] = $user_right[$k][0] == 'checkbox' ? 'array' : 'string';
				$this->db->replace('h_second_agency_right', $replace);
			}
		}
	}
}

/* End of file agency_model.php */
/* Location: ./application/models/agency_model.php */
